package com.uenpay.im.ui.old.activity.wallet;

import android.util.Log;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class ChargeByCardInvokItem extends HttpInvokeItem {

    public class ChargeByCardInvokItemResult extends HttpInvokeResult {
        public String rspCode;
        public String tradeNo;
        public String wkPayNo;
        public String resMsg;
    }

    public ChargeByCardInvokItemResult getOutput() {
        return (ChargeByCardInvokItemResult) GetResultObject();
    }

    public ChargeByCardInvokItem(String amount, String memo) {
        String url = APIConfiguration.getBaseUrl() + "Purse/Purse/charge?amount="
                + amount + "&" + APIConfiguration.getCustIdAndToken();
        Log.e("地址：", url);
        SetUrl(url);
        HashMap<String, String> requestParams = new HashMap<>();
        requestParams.put("paytype", "99");
        requestParams.put("memo", getMemoJsonString(memo));
        SetRequestParams(requestParams);
    }

    private String getMemoJsonString(String memo) {
        return "{\"bank_id\":" + memo + "}";
    }

    @Override
    protected Object DeserializeResult(String responseBody) {
        Log.e("结果：", responseBody);
        ChargeByCardInvokItemResult result = new ChargeByCardInvokItemResult();
        try {
            JSONObject json = new JSONObject(responseBody);
            result.status = json.optInt("status");
            result.msg = json.optString("msg");
            JSONObject dataJson = json.optJSONObject("data");
            if (dataJson != null && dataJson.has("body")) {
                String bodyStr = dataJson.optString("body");
                JSONObject bodyJson = new JSONObject(bodyStr);
                if (bodyJson.has("data")) {
                    JSONObject data1Json = bodyJson.optJSONObject("data");
                    if (data1Json != null && data1Json.has("rspCode") && data1Json.has("tradeNo")) {
                        result.rspCode = data1Json.optString("rspCode");
                        result.wkPayNo = data1Json.optString("wkPayNo");
                        result.tradeNo = data1Json.optString("tradeNo");
                    }
                }
                result.resMsg = bodyJson.optString("msg");
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return result;
        }

        return result;
    }

}
